package com.infoauto.opc.gateway.opc.model;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant;

/**
 * 配置接口的节点
 * */
@Getter
@Setter
public class MqttInterfaceNode {
    /**
     * 配置的接口实体
     * */
    private MqttInterface opcInterface;
    /**
     * 节点类型，label:文本 third:第三方系统 opc:opc服务
     * */
    private int nodeType;
    /**
     * 数据点的编号
     * */
    private int nodeID;
    /**
     * 节点值
     * */
    private Variant nodeValue;
    private String label;
    private int opcNodeID;//OPC节点ID
    private WebInterfaceNode webInterfaceNode;
    private String tagName;
    private long lastTime;
    private String picUrl;

    public String toString() {
        this.lastTime = System.currentTimeMillis();

        Object val = this.nodeValue==null?"":this.nodeValue.getValue();
        StringBuilder ret = new StringBuilder();
        if(val instanceof Boolean){
            ret.append((Boolean) val);
        } else if (val instanceof Short){
            ret.append((Short) val);
        } else if (val instanceof Integer){
            ret.append((Integer) val);
        } else if (val instanceof Long){
            ret.append((Long) val);
        } else if(val instanceof Float){
            ret.append((Float) val);
        } else if(val instanceof Double){
            ret.append((Double) val);
        } else if (val instanceof Byte){
            ret.append("\"");
            ret.append((Byte) val);
            ret.append("\"");
        } else if (val instanceof String){
            ret.append("\"");
            ret.append(val);
            ret.append("\"");
        } else if (val instanceof Boolean[]) {
            ret.append("[");
            ret.append(StringUtils.join((Boolean[]) val,","));
            ret.append("]");
        } else if (val instanceof Short[]) {
            ret.append("[");
            ret.append(StringUtils.join((Short[]) val,","));
            ret.append("]");
        } else if (val instanceof Integer[]) {
            ret.append("[");
            ret.append(StringUtils.join((Integer[]) val,","));
            ret.append("]");
        } else if (val instanceof Long[]) {
            ret.append("[");
            ret.append(StringUtils.join((Long[]) val,","));
            ret.append("]");
        } else if (val instanceof Float[]) {
            ret.append("[");
            ret.append(StringUtils.join((Float[]) val,","));
            ret.append("]");
        } else if (val instanceof Byte[]) {
            ret.append("[");
            for (Byte b : (Byte[])val){
                ret.append("\"");
                ret.append(b);
                ret.append("\",");
            }
            ret = ret.deleteCharAt(ret.length()-1);
            ret.append("]");
        } else if (val instanceof Double[]){
            ret.append("[");
            ret.append(StringUtils.join((Float[]) val,","));
            ret.append("]");
        } else {
            ret.append("\"");
            ret.append(val==null?"":val);
            ret.append("\"");
        }
        return "\"" + tagName + "\":{\"Value\":" + ret.toString() + ",\"Label\":\"" + (this.label==null?"":this.label) + "\",\"PicUrl\":\"" + (this.picUrl==null?"":this.picUrl) + "\",\"LastTime\":" + this.lastTime + "}";
    }
}
